package de.hs_zigr.inf.iib08gruppe5powerfish.it.entity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * is to load driver or collection string of a eXist data base
 */
public class Connection {
	/**
	 * loads driver string of eXist data base
	 * 
	 * @return driver: driver string of eXist data base
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public String getDriver() throws IOException, ClassNotFoundException {
		File file = new File("driver.dat");
		FileInputStream fileInStr;
		ObjectInputStream objectInStr;
		String strDriverL;
		if (file.exists()) {
			fileInStr = new FileInputStream("driver.dat");
			objectInStr = new ObjectInputStream(fileInStr);
			strDriverL = (String) objectInStr.readObject();
			fileInStr.close();
			objectInStr.close();
			return strDriverL;
		} else {
			return new String();
		}
	}

	/**
	 * loads collection string of eXist data base
	 * 
	 * @return strCollectionL
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public String getCollection() throws IOException, ClassNotFoundException {
		File file = new File("collection.dat");
		FileInputStream fileInStr;
		ObjectInputStream objectInStr;
		String strCollectionL;
		if (file.exists()) {
			fileInStr = new FileInputStream("collection.dat");
			objectInStr = new ObjectInputStream(fileInStr);
			strCollectionL = (String) objectInStr.readObject();
			fileInStr.close();
			objectInStr.close();
			return strCollectionL;
		} else {
			return new String();
		}
	}
	
	/**
	 * stores driver string of a eXist database
	 * 
	 * @param strDriverL
	 * @throws IOException
	 */
	public void setDriver(String strDriverL) throws IOException {
		FileOutputStream fileOutStr = new FileOutputStream("Driver.dat");
		ObjectOutputStream objektOutStr = new ObjectOutputStream(fileOutStr);
		objektOutStr.writeObject(strDriverL);
		objektOutStr.flush();
		fileOutStr.close();
		objektOutStr.close();
	}

	/**
	 * stores collection string of a eXist database
	 * 
	 * @param strCollectionL
	 * @throws IOException
	 */
	public void setCollection(String strCollectionL) throws IOException {
		FileOutputStream fileOutStr = new FileOutputStream("Collection.dat");
		ObjectOutputStream objektOutStr = new ObjectOutputStream(fileOutStr);
		objektOutStr.writeObject(strCollectionL);
		objektOutStr.flush();
		fileOutStr.close();
		objektOutStr.close();
	}
}